package com.inossem.warehousestorage_inossem.activity.receive;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.receive.extra.ReceiveExtra;
import com.inossem.warehousestorage_inossem.adapter.receive.ReceiveItemListAdapter;
import com.inossem.warehousestorage_inossem.bean.receive.ReceiveReceiveItemBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.receive.ReceiveReceiveDetailRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.ReceiveUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;

public class ReceiveReceiveItemActivity extends BaseActivity {

    private TextView title, addReceiver;

    private PullToRefreshLayout refresh;

    private PullableListView goodsList;

    private String deliveryNumberStr, receiveNumberStr, mGoodsNumber = "";

    private List<ReceiveReceiveItemBean.DataBean.GoodsListBean> allData;

    private ReceiveItemListAdapter adapter;

    private boolean mIsInsert;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_receive_receive_item);
        initView(ReceiveReceiveItemActivity.this);
        SPUtils.removeReceiveInformation(ReceiveReceiveItemActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(ReceiveReceiveItemActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.goodsListRefresh);
        goodsList = (PullableListView) activity.findViewById(R.id.goodsList);
        addReceiver = (TextView) activity.findViewById(R.id.addReceiver);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_receive_item_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_delivery_overview_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(ReceiveReceiveItemActivity.this, "退出后将清空所填信息", new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        ReceiveReceiveItemActivity.this.finish();
                    }
                }).show();
            }
        });

        goodsList.setPullUpFlag(false);
        goodsList.setPullDownFlag(false);
        getExtra(activity);
        mGoodsNumber = "";
        getData(activity, deliveryNumberStr, receiveNumberStr, mGoodsNumber);
        setmScanListener((String scanResult, int length) -> {
            mGoodsNumber = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity, deliveryNumberStr, receiveNumberStr, mGoodsNumber);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
        addReceiver.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (ReceiveUtils.hasReceiveNumber(activity)) {
                    Intent intent = new Intent(activity, ReceiveAddReceiverActivity.class);
                    intent.putExtra(ReceiveExtra.IS_INSERT, mIsInsert);
                    startActivity(intent);
                } else {
                    Utils.middleToast(activity, activity.getResources().getString(R.string.activity_receive_receive_item_fill_in_receive_item));
                }
            }
        });
    }

    private void getExtra(Activity activity) {
        deliveryNumberStr = activity.getIntent().getStringExtra(ReceiveExtra.DELIVERY_NUMBER);
        receiveNumberStr = activity.getIntent().getStringExtra(ReceiveExtra.RECEIVE_NUMBER);
        mIsInsert = activity.getIntent().getBooleanExtra(ReceiveExtra.IS_INSERT, false);
    }

    private void getData(Context context, String deliveryNumberStr, String receiveNumberStr, String goodsNumber) {
        String receiveInformation = SPUtils.getReceiveInformation(context);
        if (TextUtils.isEmpty(receiveInformation)) {
            ReceiveReceiveDetailRequest.request(context, deliveryNumberStr, receiveNumberStr, new GsonBeanListener<ReceiveReceiveItemBean>() {
                @Override
                public void onSuccess(ReceiveReceiveItemBean bean) {
                    Gson gson = new Gson();
                    String json = gson.toJson(bean, ReceiveReceiveItemBean.class);
                    SPUtils.saveReceiveInformation(context, json);
                    allData = bean.getData().getGoodsList();
                    if (allData.isEmpty()) {
                        setResult(ReceiveExtra.CREATE_CREATE_NO_GOODSLIST);
                        finish();
                    } else {
                        addGoodsListData(context, ActivityUtils.getShowData(allData, 0), goodsList, true);
                    }
                }
            });
        } else {
            Gson gson = new Gson();
            ReceiveReceiveItemBean bean = gson.fromJson(receiveInformation, ReceiveReceiveItemBean.class);
            if (TextUtils.isEmpty(goodsNumber)) {
                allData = bean.getData().getGoodsList();
            } else {
                allData = ReceiveUtils.getGoodsList(context, goodsNumber);
            }
            addGoodsListData(context, ActivityUtils.getShowData(allData, 0), goodsList, true);
        }
    }

    private void addGoodsListData(Context context, List<ReceiveReceiveItemBean.DataBean.GoodsListBean> goodsList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new ReceiveItemListAdapter(context, goodsList, mGoodsNumber, false, (String goodsNumber) -> {
                mGoodsNumber = goodsNumber;
                getData(context, deliveryNumberStr, receiveNumberStr, mGoodsNumber);
            });
            listView.setAdapter(adapter);
        }
    }

    public interface ReceiveReceiveItemListener {
        void onNoDoubleClickListener(String goodsNumber);
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(ReceiveReceiveItemActivity.this, "退出后将清空所填信息", new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                ReceiveReceiveItemActivity.super.onBackPressed();
            }
        }).show();
    }

}
